package controller

import (
	"gin_server/internal/request"
	"gin_server/internal/service"
	"gin_server/pkg/response"

	"github.com/gin-gonic/gin"
)

type LoginController struct {
	service service.UserService
}

func NewLoginController(s service.UserService) *LoginController {
	return &LoginController{service: s}
}

// Login 用户登录
// @Summary 用户登录
// @Description 用户登录接口，支持使用account或self_account进行登录
// @Tags 用户
// @Accept json
// @Produce json
// @Param entity body request.LoginRequest true "登录信息"
// @Success 200 {object} response.Response{data=service.LoginResponse}
// @Router /login/password [post]
func (c *LoginController) Login(ctx *gin.Context) {
	var req request.LoginRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	// 调用服务层进行登录验证
	result, err := c.service.Login(req.Account, req.Password)
	if err != nil {
		response.Error(ctx, err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "登录成功", result)
}

// RefreshToken 刷新token
// @Summary 刷新token
// @Description 使用refresh token刷新获取新的access token和refresh token
// @Tags 用户
// @Accept json
// @Produce json
// @Param entity body request.RefreshTokenRequest true "刷新token信息"
// @Success 200 {object} response.Response{data=service.LoginResponse}
// @Router /login/refresh [post]
func (c *LoginController) RefreshToken(ctx *gin.Context) {
	var req request.RefreshTokenRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		response.BadRequest(ctx, err.Error())
		return
	}

	result, err := c.service.RefreshToken(req.RefreshToken)
	if err != nil {
		response.Error(ctx, "token刷新失败")
		return
	}

	response.SuccessWithMessage(ctx, "token刷新成功", result)
}
